package gdconf

import (
	"server_cluster/common/logger"
)

// 队长技能配置表

// 队长技能解锁条件
const (
	CaptainSkillUnlockCondFinishQuest = 1 // 完成子任务 参数1:子任务id
	CaptainSkillUnlockCondCostItem    = 2 // 消耗道具 参数1:道具id 参数2:道具数量
)

type CaptainSkillUnlockCond struct {
	Cond    int32
	QuestId int32
	Item    Item
}

func (c *CaptainSkillUnlockCond) Parse(arr IntArray) {
	c.Cond = arr[0]
	switch c.Cond {
	case CaptainSkillUnlockCondFinishQuest:
		c.QuestId = arr[1]
	case CaptainSkillUnlockCondCostItem:
		c.Item.ItemId = arr[1]
		c.Item.ItemCount = arr[2]
	default:
	}
}

type CaptainSkill struct {
	CaptainSkillGroup int32
	UnlockCond        *CaptainSkillUnlockCond
}

type CaptainSkillConfig struct {
	RoleId int32 `csv:"NeedRole" gorm:"primary_key;autoIncrement:false"` // 角色id

	CaptainSkill1Group      int32    `csv:"RelatedSkill1Group,omitempty"` // 队长技能1组
	CaptainSkill1UnlockCond IntArray `csv:"Skill1UnlockType,omitempty"`   // 队长技能1解锁条件
	CaptainSkill2Group      int32    `csv:"RelatedSkill2Group,omitempty"` // 队长技能2组
	CaptainSkill2UnlockCond IntArray `csv:"Skill2UnlockType,omitempty"`   // 队长技能2解锁条件
	CaptainSkill3Group      int32    `csv:"RelatedSkill3Group,omitempty"` // 队长技能3组
	CaptainSkill3UnlockCond IntArray `csv:"Skill3UnlockType,omitempty"`   // 队长技能3解锁条件

	CaptainSkillList []*CaptainSkill `gorm:"-" csv:"-"` // 队长技能列表
}

func (c *CaptainSkillConfig) TableName() string {
	return "config_captain_skill"
}

func (g *GameDataConfig) saveCaptainSkillConfig() {
	saveTableToDb[CaptainSkillConfig](g.Db, readTable[CaptainSkillConfig](g.CsvPathPrefix+"CaptainSkill.csv"))
}

func (g *GameDataConfig) loadCaptainSkillConfig() {
	g.GameDataMaps.CaptainSkillConfigMap = make(map[int32]*CaptainSkillConfig)
	for _, captainSkillConfig := range loadTableFromDb[CaptainSkillConfig](g.Db) {
		if captainSkillConfig.CaptainSkill1Group != 0 {
			captainSkill := &CaptainSkill{
				CaptainSkillGroup: captainSkillConfig.CaptainSkill1Group,
				UnlockCond:        nil,
			}
			if captainSkillConfig.CaptainSkill1UnlockCond != nil {
				captainSkill.UnlockCond = new(CaptainSkillUnlockCond)
				captainSkill.UnlockCond.Parse(captainSkillConfig.CaptainSkill1UnlockCond)
			}
			captainSkillConfig.CaptainSkillList = append(captainSkillConfig.CaptainSkillList, captainSkill)
		}
		if captainSkillConfig.CaptainSkill2Group != 0 {
			captainSkill := &CaptainSkill{
				CaptainSkillGroup: captainSkillConfig.CaptainSkill2Group,
				UnlockCond:        nil,
			}
			if captainSkillConfig.CaptainSkill2UnlockCond != nil {
				captainSkill.UnlockCond = new(CaptainSkillUnlockCond)
				captainSkill.UnlockCond.Parse(captainSkillConfig.CaptainSkill2UnlockCond)
			}
			captainSkillConfig.CaptainSkillList = append(captainSkillConfig.CaptainSkillList, captainSkill)
		}
		if captainSkillConfig.CaptainSkill3Group != 0 {
			captainSkill := &CaptainSkill{
				CaptainSkillGroup: captainSkillConfig.CaptainSkill3Group,
				UnlockCond:        nil,
			}
			if captainSkillConfig.CaptainSkill3UnlockCond != nil {
				captainSkill.UnlockCond = new(CaptainSkillUnlockCond)
				captainSkill.UnlockCond.Parse(captainSkillConfig.CaptainSkill3UnlockCond)
			}
			captainSkillConfig.CaptainSkillList = append(captainSkillConfig.CaptainSkillList, captainSkill)
		}
		g.GameDataMaps.CaptainSkillConfigMap[captainSkillConfig.RoleId] = captainSkillConfig
	}
	logger.Info("CaptainSkillConfig Count: %v", len(g.GameDataMaps.CaptainSkillConfigMap))
}

func GetCaptainSkillConfig(roleId int32) *CaptainSkillConfig {
	value, exist := CONF.GameDataMaps.CaptainSkillConfigMap[roleId]
	if !exist {
		//logger.Error("[ConfigNotFound] CaptainSkillConfig, roleId: %v", roleId)
		return nil
	}
	return value
}

func GetCaptainSkillConfigMap() map[int32]*CaptainSkillConfig {
	return CONF.GameDataMaps.CaptainSkillConfigMap
}
